package com.test.service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ice.common.IceLog;
import com.ice.service.WeChatService;
import com.ice.service.impl.WeChatServiceImpl;

@Controller
@RequestMapping("/test")
public class WeChatTestController {
	
	Logger log = LoggerFactory.getLogger(WeChatTestController.class);
	@Autowired
	private WeChatService service;
	
	/**
	 * 测试获取网页授权access_token功能是否正常
	 * 需要获取腾讯服务器发送的code才能获取
	 * @return
	 */
	@RequestMapping(value="/getWebAccessToken",method=RequestMethod.GET)
	@ResponseBody
	public String testWeChatWebAccessToken(HttpServletRequest request ,HttpServletResponse response){
		String code = request.getParameter("code").trim();
		String access_token = service.getWebAuthorAccessToken(code);
		log.debug(IceLog.printLog(System.currentTimeMillis(), "测试是否可以拿到access_token", "access_token"));
		return "web_access_token"+access_token;
	}
	
	/**
	 * 测试是否可以拿到openid
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/getOpenId",method=RequestMethod.GET)
	@ResponseBody
	public String testWeChatOpenId(HttpServletRequest request ,HttpServletResponse response){
		String code = request.getParameter("code").trim();
		String openid = service.getOpenId(code);
		log.debug(IceLog.printLog(System.currentTimeMillis(), "测试是否可以拿到openid", "openid"));
		return "openid:"+openid;
	}
	
}
